package com.colin.aiyi.ui.Home.teacherSay;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.app.MyApp;
import com.colin.aiyi.dao.ImpxUtils.ImpBannerList;
import com.colin.aiyi.dao.ImpxUtils.ImpDiscourseList;
import com.colin.aiyi.dao.ImpxUtils.ImpDoLike;
import com.colin.aiyi.dao.ImpxUtils.ImpDoWatch;
import com.colin.aiyi.dao.ImpxUtils.ImpSayList;
import com.colin.aiyi.dao.ImpxUtils.ImpSayVipList;
import com.colin.aiyi.entity.BannerEntity;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.entity.DataEntity;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.response.WatchResponse;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.entity.response.YiJuAndYiShuoResponse;
import com.colin.aiyi.presenter.AbsP;
import com.colin.aiyi.ui.bannerDetail.BannerDetailAc;
import com.colin.aiyi.ui.login.LoginAc;
import com.colin.aiyi.ui.pay.PayAc;
import com.colin.aiyi.ui.sayDetail.TeacherSayDetailsActivity;
import com.colin.aiyi.ui.saySearch.SayListAc;
import com.colin.aiyi.utils.MusicUtil;
import com.colin.aiyi.utils.NetUtils;
import com.colin.aiyi.utils.SharePreferenceUtil;

import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by hl on 2017/5/8.
 */

public class ImpPSay extends AbsP implements BGABanner.Delegate{
    private boolean pullMore=false;
    private int totalCount=0;
    private int pageNum = 1;
    private int contentType=1;//1:人气 2：最新 ,已购买3
    private int lastType=1;  //上次类型
    IViewSayTeacher iViewSayTeacher;
    private User user=SharePreferenceUtil.getObject(MyApp.getInstance().getApplicationContext(),User.class);
    private YiJuAndYiShuoEntity item;
    public static final int SHOYE=0;
    public static final int  PAGE_SEARCH=1;
    private BaseQuickAdapter sayadapter;
    private int sayposition;

    public ImpPSay(IViewSayTeacher iViewSayTeacher) {
        this.iViewSayTeacher = iViewSayTeacher;
    }





    @Override
    public void onSuccess(Object t) {
        if (t instanceof WatchResponse){
            if (((WatchResponse) t) .getCode()==200)
            {
                watch_action(item, (WatchResponse) t);
            }else {
                sendMsgToView(((BaseResponse)t));
            }
            return;
        }

        if (((YiJuAndYiShuoResponse) t).getCode()==200)
        {
            dfd((YiJuAndYiShuoResponse) t);
        }else {
            sendMsgToView(((BaseResponse)t));
        }
    }




    private void dfd(YiJuAndYiShuoResponse d){
        DataEntity dataEntity = d.getData();
        totalCount=dataEntity.getCount();

        List<YiJuAndYiShuoEntity> list = dataEntity.getItems();
        if (lastType!=contentType)
        {
            pullMore=false;
        }
        lastType=contentType;

        if (pullMore)
        {
            iViewSayTeacher.pullMoreComlite();
         }else {
            iViewSayTeacher.getData().clear();
            iViewSayTeacher.refreshComplite();
        }
        if (list!=null&&list.size()>0)
        {
            iViewSayTeacher.getData().addAll(list);
        }else {
            iViewSayTeacher.refreshNoData();
        }

    }

    public void getSayList(int type,BaseRequest request) {
        request.setTitle(iViewSayTeacher.getSearchContent());
        switch (type)
        {
            case 1:
            case 2:
                say_list(request);
                break;
            case 3:
                say_vipList(request);
                break;
        }
    }


    @Override
    public <V> void activity_list(V v) {
        super.activity_list(v);
        iService= new ImpBannerList();
        iService.getObject(v);
    }


    @Override
    public <V> void discourseList(V v) {
        super.discourseList(v);
        iService=new ImpDiscourseList();
        iService.getObject(v);
    }

    @Override
    public <V> void say_vipList(V v) {
        iService= new ImpSayVipList();
        iService.getObject(this,v);

    }

    @Override
    public <V> void say_list(V v) {
        iService= new ImpSayList();
        iService.getObject(this,v);
    }

    /*********
     * 观看成功后播放音乐
     * @param item
     * @param response
     */
    private void watch_action(YiJuAndYiShuoEntity item,WatchResponse response){
        if (item==null)
            return;
        MusicUtil.getInstance().setMusicTitle(item.getTitle());
        MusicUtil.getInstance().setMusicPic(Constant.BASE_URL+item.getUrl());
        MusicUtil.getInstance().setDataSource( item.getVoiceUrl()+response.getData().getToken());
        item.setWatchCount(item.getWatchCount()+1);
        item.setWatchCountActive(1);
        sayadapter.notifyItemChanged(sayposition+sayadapter.getHeaderLayoutCount(),item);
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        this.sayadapter=adapter;
        this.sayposition=position;
        User user1=SharePreferenceUtil.getObject(MyApp.getInstance().getApplicationContext(),User.class);
        item = (YiJuAndYiShuoEntity) adapter.getData().get(position);
        switch (view.getId()) {
            case  R.id.img_video_play:
            case  R.id.img_video:

                if (user1==null||user1.getAccessToken()==null||user1.getAccessToken().length()<1){
                    iViewSayTeacher.startNextAc(new Intent(iViewSayTeacher.getContext(),LoginAc.class));
                    return;
                }else {
                    if (!item.isIsFree()&&!user.isVip()){
                        iViewSayTeacher.showMsg("本条需要VIP,请充值");
                        iViewSayTeacher.startNextAc(new Intent(iViewSayTeacher.getContext(),PayAc.class));
                        return;
                    }
                }

                User localUser=SharePreferenceUtil.getObject(iViewSayTeacher.getContext(),User.class);
                if (NetUtils.isMobileNetWork(iViewSayTeacher.getContext())&&!localUser.isListen()){
                    Toast.makeText(iViewSayTeacher.getContext(),"您已设置流量模式禁止收听",Toast.LENGTH_SHORT).show();
                    return;
                }
                MusicUtil.getInstance().setMusicTitle(item.getTitle());
                MusicUtil.getInstance().setMusicPic(Constant.BASE_URL+item.getUrl());
                user.setSayId(item.getId()+"");
                say_watch(user);
                MusicUtil.getInstance().sendStartEvent();
                break;

            case R.id.text_like:
                if (user==null||user.getAccessToken()==null||user.getAccessToken().length()<1){
                    iViewSayTeacher.startNextAc(new Intent(iViewSayTeacher.getContext(),LoginAc.class));
                    return;
                }
                int a= ((TextView)view).getCompoundDrawables()[0].getLevel();
                int sum= Integer.parseInt(((TextView) view).getText().toString());
                sum++;
                a++;
                boolean operType=true;
                if (a==2)
                {
                    sum-=2;
                    a-=2;
                    operType=false;
                }
                ((TextView) view).setText(sum+"");
                ((TextView)view).getCompoundDrawables()[0].setLevel(a);
                user.setOperType(operType);
                user.setOrderType("1");
                user.setSayId(item.getId()+"");
                like_doLike(user);
             //   ImpOther.getInstance().doLike(operType,item.getId()+"","1");
                break;
            case R.id.text_watch:
            case R.id.text_reply:
                Intent intent = new Intent(iViewSayTeacher.getContext(), TeacherSayDetailsActivity.class);
                String id=((YiJuAndYiShuoEntity) adapter.getData().get(position)).getId()+"";
                intent.putExtra("id",id);
                iViewSayTeacher.startNextAc(intent);
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        super.onItemClick(adapter, view, position);
        Intent intent = new Intent(iViewSayTeacher.getContext(), TeacherSayDetailsActivity.class);
        String id=((YiJuAndYiShuoEntity) adapter.getData().get(position)).getId()+"";
        String url=Constant.BASE_URL+((YiJuAndYiShuoEntity) adapter.getData().get(position)).getUrl();
        intent.putExtra("id",id);
        intent.putExtra("imgurl",url);
        iViewSayTeacher.startNextAc(intent);


    }

    public void onRefresh() {
        pullMore=false;
        user.setPageNum(1);
        user.setTitle("");
        getSayList(contentType,user);
        if (iViewSayTeacher.getPageType()==SHOYE){
            activity_list(user);
        }
        BaseRequest baseRequest = new BaseRequest();
        baseRequest.setPageSize("1");
        discourseList(baseRequest);
    }

    @Override
    public void onLoadMoreRequested() {

        if (iViewSayTeacher.getData().size() < totalCount) {
            pullMore=true;
            user.setPageNum(user.getPageNum()+1);
            user.setTitle(iViewSayTeacher.getSearchContent());
            getSayList(contentType,user);
        } else {
            iViewSayTeacher.pullMoreNoMore();

        }
    }

    @Override
    public void onTabSelect(int position) {
        pullMore=false;
        switch (position)
        {
            case 0:
                this.contentType = 2;
                break;
            case 1:
                this.contentType = 1;
                break;
            case 2:
                this.contentType = 3;
                break;
        }
        user.setPageNum(1);
        user.setTitle(iViewSayTeacher.getSearchContent());
        user.setOrderType(contentType+"");
        if (contentType==3){
            User user= SharePreferenceUtil.getObject(iViewSayTeacher.getContext(),User.class);
            if (user==null||user.getAccessToken()==null||user.getAccessToken().length()<1)
            {
                Intent intent=new Intent(iViewSayTeacher.getContext(), LoginAc.class);
                intent.putExtra("FromAc", Constant.MIAN);
                iViewSayTeacher.startNextAc(intent);
                return;
            }else {
                user.setAccessToken(user.getAccessToken());
            }
        }
       getSayList(contentType,user);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.text_search:
            if (iViewSayTeacher.getPageType()==PAGE_SEARCH){
                user.setTitle(iViewSayTeacher.getSearchContent());
                getSayList(contentType,user);
            }else {
                Intent intent=new Intent(iViewSayTeacher.getContext(),SayListAc.class);
                intent .putExtra("contentType",contentType);
                intent.putExtra("searchContent",iViewSayTeacher.getSearchContent());
                iViewSayTeacher.startNextAc(intent);
                iViewSayTeacher.removeSearchContent();


            }

            break;
        }
    }


    @Override
    public <V> void like_doLike(V v) {
        super.like_doLike(v);
        iService=new ImpDoLike();
        iService.getObject(user);
    }

    @Override
    public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
        Intent intent =new Intent(iViewSayTeacher.getContext(), BannerDetailAc.class);
        BannerEntity.DataBean bean= (BannerEntity.DataBean) model;
        intent.putExtra("id",bean.getId());
        intent.putExtra("title",bean.getTitle());
        intent.putExtra("headurl",bean.getUrl());
        iViewSayTeacher.startNextAc(intent);
    }

    /********
     * 观看接口
     * @param v
     * @param <V>
     */
    @Override
    public <V> void say_watch(V v) {
        super.say_watch(v);
        iService=new ImpDoWatch();
        iService.getObject(this,v);
    }
}
